package com.capybara.account.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.capybara.account.model.dto.AuUserDTO;
import com.capybara.account.model.entity.AuUser;
import com.capybara.account.model.query.AuUserQuery;
import com.capybara.account.model.vo.AuUserVO;
import com.capybara.account.result.Result;
import com.capybara.account.result.ResultPage;
import com.capybara.account.result.ResultSet;
import com.capybara.account.service.IAuUserService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.Principal;
import java.util.List;

/**
 * 用户
 *
 * @author wgc
 * @date 2024-09-12
 */
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("/account/auUser")
public class AuUserController {
    private final IAuUserService userService;

    /**
     * 查询全部
     */
    @GetMapping()
//    @PreAuthorize("hasAuthority('SCOPE_web')")
    public ResultPage<AuUserVO> findAll(AuUserQuery param,
                                        Principal principal) {
        Page<AuUserVO> page = param.joinQuery(AuUser.class).page(AuUserVO.class);
        return ResultSet.page(page);
    }

    /**
     * 查询全部无分页
     */
    @GetMapping("findAllNoPaging")
    public Result<String> findAllNoPaging(@RequestParam @Validated String name) {
//        List<AuUserVO> dtos = param.joinQuery(AuUser.class).listDeep(AuUserVO.class);
        return ResultSet.go("dtos");
    }

    /**
     * 查询单个
     */
    @GetMapping("getById")
    public Result<AuUser> getById(@RequestParam String id) {
        AuUser byId = userService.getById(id);
        return ResultSet.go(byId);
    }

    /**
     * 保存-新增
     */
    @PostMapping()
    public Result<?> save(@RequestBody @Validated AuUserDTO param) {
        boolean b = userService.save(param);
        return ResultSet.operation(b);
    }

    /**
     * 保存-修改
     */
    @PutMapping()
    public Result<?> updateById(@RequestBody @Validated AuUserDTO param) {
        boolean b = userService.updateById(param);
        return ResultSet.operation(b);
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public Result<?> del(@RequestParam List<String> ids) {
        boolean b = userService.removeByIds(ids);
        return ResultSet.operation(b);
    }

    /**
     * 导出
     */
    @GetMapping("export")
    public void export(AuUserQuery param, HttpServletResponse response) throws IOException {
        userService.export(param, response);
    }

}

